package cn.tedu.redissondemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class stringRedisTemplateTest1 {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void testRedis(){

        //获取一条数据,源数据不变

        stringRedisTemplate.opsForValue().set("aa","aa");
        String aa = stringRedisTemplate.opsForValue().get("aa");
        stringRedisTemplate.opsForValue().set("test", "100",60*10, TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
        System.out.println(aa);
    }

    @Test
    public void redisList(){
        // 从缓存中取出
        // https://blog.csdn.net/baidu_17353319/article/details/88976087
        String key = "redis-list-key";
        String value = String.valueOf(System.currentTimeMillis() + 1);
        stringRedisTemplate.opsForList().leftPush(key,value);
        stringRedisTemplate.opsForList().leftPush(key,value);
        stringRedisTemplate.opsForList().leftPush(key,value);
        String s = stringRedisTemplate.opsForList().leftPop(key);
        String s1 = stringRedisTemplate.opsForList().leftPop(key);
        String s2 = stringRedisTemplate.opsForList().leftPop(key);
        System.out.println(s);
    }

    @Test
    public void redisSet(){
        // 从缓存中取出
        // set元素不能重复  value不能重复
        // https://blog.csdn.net/baidu_17353319/article/details/88994844
        String key = "redis-set-key";
        String value = String.valueOf(System.currentTimeMillis() + 1);
        stringRedisTemplate.opsForSet().add(key,value);
        stringRedisTemplate.opsForSet().add(key,value+"-1");
        stringRedisTemplate.opsForSet().add(key,value+"-2");
        //System.out.println(stringRedisTemplate.opsForSet().pop(key));
    }

    @Test
    public void redisHash(){
        // API文档: https://blog.csdn.net/u010277958/article/details/94590220
        //获取一条数据,源数据不变
        String key = "redis-hash-key";
        String hashKey = String.valueOf(System.currentTimeMillis());
        String value = String.valueOf(System.currentTimeMillis() + 1);
        stringRedisTemplate.opsForHash().put(key,hashKey,value);

        // redisTemplate.opsForHash().put("hashValue","map1","map1-1");
        // redisTemplate.opsForHash().put("hashValue","map2","map2-2");
        System.out.println(stringRedisTemplate.opsForHash().get(key,hashKey));
    }

    @Test
    public void redisZSet(){
        //获取一条数据,源数据不变
        // https://blog.csdn.net/baidu_17353319/article/details/89003873
        String key = "redis-zset-key";
        String hashKey = String.valueOf(System.currentTimeMillis());
        stringRedisTemplate.opsForZSet().add(key,hashKey,System.currentTimeMillis());
        System.out.println(stringRedisTemplate.opsForZSet().zCard(key));
    }
}
